package vip.xiaonuo.client.modular.wxuser.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jodd.util.StringUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.biz.modular.user.entity.BizFzUser;
import vip.xiaonuo.biz.modular.user.param.*;
import vip.xiaonuo.biz.modular.user.service.BizFzUserService;
import vip.xiaonuo.client.modular.wxpay.service.MyWxPayService;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.util.CommonCryptogramUtil;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "微信分账用户控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 2)
@RequestMapping("/client/c/fzuser")
public class ClientFzUserController {
    @Resource
    private BizFzUserService bizFzUserService;
    @Resource
    private MyWxPayService myWxPayService;

    /**
     * 编辑分账用户表
     *
     * @author gtc
     *
     */
    @ApiOperation(value = "编辑分账用户表", position = 3)
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Validated BizFzUserEditParam bizFzUserEditParam) {
        bizFzUserService.edit(bizFzUserEditParam);
        return CommonResult.ok();
    }

    /**
     * 获取分账用户表详情
     *
     * @author gtc
     *
     */
    @ApiOperation(value = "获取分账用户表详情", position = 5)
    @GetMapping("/detail")
    public CommonResult<BizFzUser> detail(BizFzUserIdParam bizFzUserIdParam) {
        return CommonResult.data(bizFzUserService.detail(bizFzUserIdParam));
    }

    /**
     * 绑定微信小程序openid
     *
     * @author gtc
     *
     */
    @ApiOperation(value = "绑定微信小程序openid", position = 6)
    @PostMapping("/bindWxOpenId")
    public CommonResult<Boolean> bindWxOpenId(@RequestBody @Validated BizFzUserWxBindParam param) {
        return CommonResult.data(bizFzUserService.bindWxOpenId(param));
    }

    /**
     * 微信小程序登录
     *
     * @author xiao
     */
    @ApiOperation(value = "微信小程序登录", position = 7)
    @PostMapping("/wxLogin")
    public CommonResult<Map<String, Object>> wxLogin(@RequestBody @Validated BizFzUserWxLoginParam param) {
        Map<String, Object> result = bizFzUserService.wxLogin(param);
        if (result == null) {
            return CommonResult.error("您不具备分销用户权限，请联系管理员");
        }
        return CommonResult.data(result);
    }

    @ApiOperation("测试用登录")
    @PostMapping("/login")
    public CommonResult<String> login(@RequestBody @Valid ClientUserLoginParam loginParam){
        return CommonResult.data(bizFzUserService.login(loginParam));
    }

    /**
     * 注册分销用户
     *
     * @author xiao
     */
    @ApiOperation(value = "注册分销用户", position = 8)
    @PostMapping("/register")
    public CommonResult<Map<String, Object>> register(@RequestBody @Validated BizFzUserRegisterParam param) {
        Map<String, Object> result = bizFzUserService.register(param);
        if (result != null) {
            if (StrUtil.isNotBlank((String)result.get("token"))) {
                // 增加分销人绑定
                myWxPayService.addProfitSharingReceiver((String)result.get("openId"), "PARTNER");
            }
        }
        return CommonResult.data(result);
    }

    /**
     * 获取分账用户邀请码
     *
     * @author gtc
     *
     */
    @ApiOperation(value = "获取分账用户邀请码", position = 9)
    @GetMapping("/getInviteCode")
    public CommonResult<String> getInviteCode(BizFzUserIdParam bizFzUserIdParam) {
        // 获取用户ID
        String userId = bizFzUserIdParam.getId();
        // 验证用户是否存在
        BizFzUser user = bizFzUserService.queryEntity(userId);
        if (user == null) {
            return CommonResult.error("用户不存在");
        }
        // 使用SM2加密用户ID作为邀请码
        String inviteCode = CommonCryptogramUtil.doSm2Encrypt(userId);
        return CommonResult.data(inviteCode);
    }

    /**
     * 获取全量分账用户列表
     *
     * @author gtc
     *
     */
    @ApiOperation(value = "获取全量分账用户列表", position = 10)
    @GetMapping("/list")
    public CommonResult<List<BizFzUser>> list(BizFzUserPageParam bizFzUserPageParam) {
        // 创建查询条件
        QueryWrapper<BizFzUser> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(bizFzUserPageParam.getName())) {
            queryWrapper.lambda().like(BizFzUser::getName, bizFzUserPageParam.getName());
        }
        if(ObjectUtil.isNotEmpty(bizFzUserPageParam.getPhone())) {
            queryWrapper.lambda().like(BizFzUser::getPhone, bizFzUserPageParam.getPhone());
        }
        if(ObjectUtil.isNotEmpty(bizFzUserPageParam.getStatus())) {
            queryWrapper.lambda().eq(BizFzUser::getStatus, bizFzUserPageParam.getStatus());
        }
        if(ObjectUtil.isNotEmpty(bizFzUserPageParam.getFzRole())) {
            queryWrapper.lambda().eq(BizFzUser::getFzRole, bizFzUserPageParam.getFzRole());
        }
        if(ObjectUtil.isNotEmpty(bizFzUserPageParam.getFzType())) {
            queryWrapper.lambda().eq(BizFzUser::getFzType, bizFzUserPageParam.getFzType());
        }

        // 直接调用service的list方法获取全部数据
        List<BizFzUser> list = bizFzUserService.list(queryWrapper);
        return CommonResult.data(list);
    }
}
